package com.woniu.shop.schedule;

import com.woniu.CommentFeignClient;
import com.woniu.shop.pojo.Shop;
import com.woniu.shop.service.IShopService;
import com.woniu.vo.CommentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class ShopSchedule {

    @Autowired
    private IShopService shopService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private CommentFeignClient commentFeignClient;

    /**
     * 定时扫描每天的信誉分
     * 1.信誉分低于95分，发送rb警告
     * 2.若是发现封禁日期大于当前日期，解封并且加5分信誉分
     */
//    @Scheduled(cron = "0/10 * * * * ? ")
    public void scan(){
        log.info("定时扫描信誉分");
        List<Shop> shops = shopService.list();
        for (Shop shop : shops) {
            if(shop.getScore()<95 && shop.getScore()>=90){
                rabbitTemplate.convertAndSend("message-shop-queue",shop.getId()+"您的信誉分已经低于95，若是低于90则会封禁一月，请注意");
            }
            if(shop.getStatus()==2 && shop.getClosingTime().compareTo(new Date())<1){
                shop.setStatus(1);
                shop.setScore(shop.getScore()+5);
                Integer score = shop.getScore();
                if(score/5==0){
                    score=score+1;
                }
                if(shop.getScore()<90){
                    shop.setStatus(0);
                    rabbitTemplate.convertAndSend("message-shop-queue",shop.getId() + "您的信誉分为" + shop.getScore() +
                                                          "，还需要封禁" + (95-score)/5 + "个月");
                }
                shopService.updateById(shop);
            }
        }
    }

    /**
     * 定时扫描每月的信誉分,低于90分（每5分封1个月）封店
     */
//    @Scheduled(cron = "0 0/1 * * * ? ")
    public void scanStop(){
        List<Shop> shops = shopService.list();
        log.info("定时扫描每月的信誉分,低于90分（每5分封1个月）封店");
        shops.stream().forEach(shop -> {
            if(shop.getScore()<90){
                shop.setStatus(2);
                shop.setClosingTime(new Date());
                shopService.updateById(shop);
                rabbitTemplate.convertAndSend("message-shop-queue",shop.getId()+"您的信誉分已经低于90,封禁一个月");
            }
        });
    }

    /**
     * 定时扫描每月的评价，低于3则扣除1分信誉分
     */
//    @Scheduled(cron = "0/30 * * * * ? ")
    public void scanComment(){
        log.info("定时扫描每月的评价，低于3则扣除1分信誉分");
        Date date = new Date();
        int month = date.getMonth();
        System.out.println(date);
        date.setMonth(date.getMonth()-1);
        System.out.println(date);
        List<CommentVo> commentVos = commentFeignClient.getShopAvgComment(date).getData();
        commentVos.forEach(s->{
            if(s.getAvgLevel().compareTo(new BigDecimal(3))<1){
                Shop shop = shopService.getById(s.getShopId());
                shop.setScore(shop.getScore()-1);
                shopService.updateById(shop);
            }
        });
    }
}
